package model.loader;

import java.io.File;
import java.util.ArrayList;

import model.Food;
import model.utils.FileUtils;
import jxl.Sheet;
import jxl.Workbook;

import com.thoughtworks.xstream.XStream;

public class ExcelInfoLoader {
	
	
	public ArrayList<Food> loadExcelInfo(){
		String path = "src\\excel\\calorias.xls";
		ArrayList<Food> food = new ArrayList<Food>();
		try {
			Workbook workbook = Workbook.getWorkbook(new File(path));
			Sheet sheet = workbook.getSheet(1);
			
			int row = 0; 
			String foodName;
			int foodCalories; 
			while(!sheet.getCell(0, row).getContents().equals("")){
				if(row == 0){
					row++;
					continue;
				}
				foodName = sheet.getCell(0, row).getContents();
				foodCalories = new Integer(sheet.getCell(1, row).getContents());
				Food newFood = new Food(foodName, foodCalories);
				food.add(newFood);
				row++;
			}
		}catch(ArrayIndexOutOfBoundsException e1){
			//nothing		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return food; 
	}
	
	

	public static void main(String[] args) {
		
		ExcelInfoLoader loader = new ExcelInfoLoader(); 
		
		ArrayList<Food> foods = loader.loadExcelInfo();
		
		XStream xstream = new XStream();
		xstream.alias("food", Food.class);
		String xmlText = xstream.toXML(foods);
		
		FileUtils.writeFile("calories.xml", xmlText);		
	}

}
